package com.doudou.pay.helper.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.doudou.pay.helper.commons.PayRedisKey;
import com.doudou.pay.helper.entities.PayUser;
import com.doudou.pay.helper.mapper.PayUserMapper;
import com.doudou.pay.helper.service.PayUserService;

@Service("payUserService")
public class PayUserServiceImpl implements PayUserService {
	
	@Autowired
	private PayUserMapper payUserMapper;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public List<PayUser> getPayUserByUserId(String userId) {
		return payUserMapper.selectPayUserByUserId(userId);
	}

	@Override
	public PayUser getPayUserByAuthToken(String authToken) {
		return payUserMapper.selectPayUserByAuthToken(authToken);
	}

	@Override
	public Map<String, Integer> getOnlineAuthToken(String userId) {
		Map<String, Integer> onlineMap = new HashMap<String, Integer>();
		
		Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(PayRedisKey.getUserTokenHashKey(userId));
		Set<Object> authTokenSet = entries.keySet();
		for(Object authToken:authTokenSet) {
			Object useCount = entries.get(authToken);
			onlineMap.put(String.valueOf(authToken), Integer.parseInt(String.valueOf(useCount)));
		}
		
		return onlineMap;
	}

	@Override
	public int updateNotifyUrl(String authToken, String notifyAddress) {
		return payUserMapper.updateNotifyAddress(authToken, notifyAddress);
	}

}
